#include "FarmStruct.h"
#include "Level.h"
#include "WorkerUnit.h"
#include "ImgMgr.h"
#include "DirtTile.h"
#include "FieldStruct.h"

FarmStruct::FarmStruct() : Struct()
{
}

std::string FarmStruct::getRessourceStruct() const
{
	return "Ble";
}

void FarmStruct::init(const sf::Vector2f position, const int team, ImgMgr *imageManager)
{
	m_skin = imageManager->getSprite(10);
	m_skin.setScale(6, 6);
	m_vie = 5;
	m_nombreMaxWorker = 2;
	Struct::init(sf::Vector2f((float)random((int)position.x - 8, (int)position.x + 8), (float)random((int)position.y - 8, (int)position.y + 8)), team, imageManager);
}

void FarmStruct::maj(Level *level, RessourceMgr *ressourceMgr)
{
	if(m_clockField.getElapsedTime().asMilliseconds() > 50)
	{
		int e  = 0;
		while(e < 100)
		{
			if(m_field[e].isDestroy())
			{
				int posX = random(getPosition().x + m_skin.getGlobalBounds().width / 2 - 75, getPosition().x + m_skin.getGlobalBounds().width / 2 + 75);
				int posY = random(getPosition().y + m_skin.getGlobalBounds().height / 2 - 75, getPosition().y + m_skin.getGlobalBounds().height / 2 + 75);
				while(posX + 8 > getPosition().x && posX < getPosition().x + m_skin.getGlobalBounds().width && posY + 8 > getPosition().y && posY < getPosition().y + m_skin.getGlobalBounds().height)
				{
					posX = random(getPosition().x + m_skin.getGlobalBounds().width / 2 - 75, getPosition().x + m_skin.getGlobalBounds().width / 2 + 75);
					posY = random(getPosition().y + m_skin.getGlobalBounds().height / 2 - 75, getPosition().y + m_skin.getGlobalBounds().height / 2 + 75);
				}
				level->addRessourceStruct(&m_field[e], sf::Vector2f((float)posX, (float)posY));
				m_clockField.restart();
				return;
			}
			e++;
		}
		m_clockField.restart();
	}
	Struct::maj(level, ressourceMgr);
}